// pages/work-order/orderList/index.ts
import {page} from '../../../api/work_order'
import { LENGYU_SUCCESS_CODE,PageRequestParam } from '../../../utils/request';
import { STORAGE_KEY } from "../../../api/config";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [ 
      { "text": "所有",value:0},
      { "text": "我发起的",value:1},
      { "text": "我参与的",value:2}
    ],
    userDialogShow:false,
    selectedTab:0,
    pageParam:{
      pageIndex:1,
      pageSize:10,
      paramMapObj:{
        vx_open_id:"",
        order_type:"",
        current_status:"",
        searchType:0
      }
    },
    queryUserName:"",
    searchOver:false,
    refresherTriggered:false,
    orderList:[
    ],
    roles:wx.getStorageSync(STORAGE_KEY.ROLES),
    adminRoleFlag:false,
    sendOrderRoleFlag:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option:any) {
    console.log("列表页面参数",option)
    if(option && option.type){
      this.setData({
        'pageParam.paramMapObj.searchType':option.type,
        selectedTab:option.type,
      })
    }
    else{
      this.setData({
        'pageParam.paramMapObj.searchType':this.data.tabs[0].value,
        selectedTab:0,
      })
    }
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      adminRoleFlag:this.data.roles.indexOf("管理员") > -1,
      sendOrderRoleFlag:this.data.roles.indexOf("管理员") > -1 || this.data.roles.indexOf("派单人员") > -1
    })
    this.queryData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

/**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  loadMore() {
    console.log("上拉加载")
    this.getPageList(false);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onRefresh() {
    console.log("下拉刷新")
    console.log(this.data.refresherTriggered);
    this.setData({
      'pageParam.pageIndex':1
    })
    this.getPageList(true);
    setTimeout(()=>{
      this.setData({
        'refresherTriggered':false
      })
    },1000)
  
    console.log(this.data.refresherTriggered);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  selectUser(){
    console.log("选择用户点击事件")
    this.data.pageParam.paramMapObj.vx_open_id
    this.setData({
      // 'pageParam.paramMapObj.vx_open_id':"",
      userDialogShow:true
    })
  },
  bindUserSeleted(e:any){
    // console.log("选择了用户",e)
    this.setData({
      'pageParam.paramMapObj.vx_open_id':e.detail.value.vxOpenId,
      'queryUserName':e.detail.value.vxNickName
    })
    this.queryData()
  },
  orderTypeChanged(e:any){
    this.setData({
      'pageParam.paramMapObj.order_type':e.detail.value,
    })
    this.queryData()
  },
  orderStatusChanged(e:any){
    this.setData({
      'pageParam.paramMapObj.current_status':e.detail.value,
    })
    this.queryData()
  },
  async getPageList(refresh:boolean){
      if(!refresh&& this.data.searchOver){
        console.log("已经查询完成不在继续查询")
        return;
      }
      let result = await page(this.data.pageParam as PageRequestParam);
      console.log(result);
      if(result.data.records.size > this.data.pageParam.pageSize){
        this.setData({
          'pageParam.pageIndex':this.data.pageParam.pageIndex+1
        })
      }
      else{
        this.setData({
          'searchOver':true
        })
      }
      if(refresh){
        this.setData({
          'orderList':result.data.records
        })
      }
      else{
        this.setData({
          'orderList':this.data.orderList.concat(result.data.records)
        })
      }

      console.log(this.data.orderList)
  },
  gotoDetail(e:any){
    console.log("进入详情页面：" , e.currentTarget.dataset.id);
    wx.navigateTo({
      url:"/pages/work-order/order-detail/index?id=" + e.currentTarget.dataset.id,
    })
  },
  switchTab(e:any) {
    console.log("tab点击",e);
    let index = e.currentTarget.dataset.index
   
    this.setData({
      selectedTab: index,
      'pageParam.paramMapObj.searchType':this.data.tabs[index].value
    });
    this.queryData()
  },
  clearQueryParam(){
    console.log("条件清除")
    this.setData({
      'pageParam.paramMapObj.vx_open_id':"",
      'queryUserName':"",
      'pageParam.paramMapObj.order_type':"",
      'pageParam.paramMapObj.current_status':"",
    })
    this.queryData()
  },
  queryData(){
    this.onRefresh();
  }
})